package com.small.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.small.account.mapper.UserAndRoleMapper;
import com.small.account.entity.UserAndRole;
import com.small.account.service.IUserAndRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**

 * 服务实现类

 *
 * @author wangguochao
 * @since 2020-01-06
 */
@Service
public class UserAndRoleServiceImpl extends ServiceImpl<UserAndRoleMapper, UserAndRole> implements IUserAndRoleService {
    @Resource
    private UserAndRoleMapper userAndRoleMapper;
    /**
     * 用户角色修改新增
     *
     * @param userId 用户id
     * @param roles  角色
     */
    @Override
    public void userAddRole(String userId, List<String> roles) {
        if (roles != null && roles.size() > 0) {
            QueryWrapper<UserAndRole> wrapper = new QueryWrapper<UserAndRole>();
            wrapper.eq("user_id", userId);
            remove(wrapper);
            List<UserAndRole> list = new ArrayList<>();
            for (String role : roles) {
                UserAndRole userAndRole = new UserAndRole();
                userAndRole.setRoleId(role);
                userAndRole.setUserId(userId);
                list.add(userAndRole);
            }
            saveOrUpdateBatch(list);
        }
    }

    @Override
    public List<UserAndRole> findByUserId(Set<String> ids) {
        return userAndRoleMapper.findByUserId(ids);
    }
}
